package com.eightbears.beboy.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.eightbears.beboy.R;
import com.eightbears.beboy.base.util.CommonUtils;
import com.eightbears.beboy.base.util.GetCategory;
import com.eightbears.beboy.base.util.SPUtil;
import com.eightbears.beboy.base.view.TextViewExpandableAnimation;
import com.eightbears.beboy.base.view.image.view.ImageItem;
import com.eightbears.beboy.base.view.layout.WrappableGridLayoutManager;
import com.eightbears.beboy.entity.FriendCirleEntity;
import com.eightbears.beboy.entity.HomeListEntity;
import com.eightbears.beboy.entity.UserEntity;
import com.makeramen.roundedimageview.RoundedImageView;

import java.util.List;


/**
 * Created by Bao on 2016/12/7.
 * 首页适配器
 */
public class CircleListAdapter extends BaseQuickAdapter<FriendCirleEntity.DataBean.ListBean, BaseViewHolder> {

    public static final String STATUS_BUY_CONTENT = "buyContent";
    public static final String STATUS_SHOW_CONTENT = "showContent";

    private RecyclerView rv_img;
    private ImageView iv_shop_icon;
    private View view_pay;

    private UserEntity mUserEntity;

    private OnClickImageItemListener mClickImageListener;

    // 图片查看单击事件
    public interface OnClickImageItemListener {
        void onImageClick(BaseQuickAdapter baseViewHolder, View view, int i);
    }

    public void setOnImageClickListener(final OnClickImageItemListener clickListener) {
        this.mClickImageListener = clickListener;
    }


    public CircleListAdapter(Context context) {
        super(R.layout.item_cirlce_list, null);
        this.mContext = context;
        if (null == mUserEntity)
            mUserEntity = SPUtil.getUser();
    }

    @Override
    protected void convert(final BaseViewHolder baseViewHolder, FriendCirleEntity.DataBean.ListBean listBean) {
        // 设置基本数据
        initBaseData(baseViewHolder, listBean);
        // 图片数据
        showImages(baseViewHolder, listBean);

    }


    // 显示图片
    private void showImages(BaseViewHolder baseViewHolder, FriendCirleEntity.DataBean.ListBean listBean) {
        if (listBean.getImages() != null && listBean.getImages().size() > 0) {
            View imagesInfo = baseViewHolder.getView(R.id.view_image);
            rv_img = (RecyclerView) imagesInfo.findViewById(R.id.rv_img);
            imagesInfo.setVisibility(View.VISIBLE);
            initImagesAdapter(listBean.getImages());
        }
    }

    /**
     * 设置基本信息
     */
    private void initBaseData(BaseViewHolder baseViewHolder, FriendCirleEntity.DataBean.ListBean listBean) {
//        CommonUtils.loadImageView(mContext, listBean.getUser().getCover(), R.mipmap.default_head,
//                (RoundedImageView) baseViewHolder.getView(R.id.iv_user_head));
        ImageView iv_level = baseViewHolder.getView(R.id.iv_level);
        ImageView iv_vip = baseViewHolder.getView(R.id.iv_vip);
        if (listBean.getUser() != null) {
            Glide.with(mContext).load(listBean.getUser().getCover()).error(R.mipmap.default_head).into((RoundedImageView) baseViewHolder.getView(R.id.iv_user_head));
            baseViewHolder.setText(R.id.tv_nickname, listBean.getUser().getNickname());
            if (listBean.getUser().getVip_level() == 1) {
                iv_vip.setVisibility(View.VISIBLE);
                iv_vip.setImageResource(R.mipmap.icon_new_vip1);
            } else if (listBean.getUser().getVip_level() == 2) {
                iv_vip.setVisibility(View.VISIBLE);
                iv_vip.setImageResource(R.mipmap.icon_new_vip2);
            } else {
                iv_vip.setVisibility(View.GONE);
            }
        }

        if (listBean.getPoints() != null) {
            if (listBean.getPoints().getLevel() == 1) {
                iv_level.setImageResource(R.mipmap.icon_level_one);
            } else if (listBean.getPoints().getLevel() == 2) {
                iv_level.setImageResource(R.mipmap.icon_level_two);
            } else if (listBean.getPoints().getLevel() == 3) {
                iv_level.setImageResource(R.mipmap.icon_level_three);
            } else if (listBean.getPoints().getLevel() == 4) {
                iv_level.setImageResource(R.mipmap.icon_level_four);
            } else if (listBean.getPoints().getLevel() == 5) {
                iv_level.setImageResource(R.mipmap.icon_level_five);
            }
            iv_level.setVisibility(View.VISIBLE);
        }
        baseViewHolder.setText(R.id.tv_publish_date, CommonUtils.TimeStamp2Date(listBean.getCreated_at(), "MM-dd HH:mm"))
                .addOnClickListener(R.id.iv_user_head)
                .addOnClickListener(R.id.ll_follow_drop_down);
        String content = listBean.getContent();


        TextViewExpandableAnimation contentText = baseViewHolder.getView(R.id.tv_content);
        contentText.setText(content);
        contentText.resetState(true);
    }

    /**
     * 初始化图片适配
     */
    private void initImagesAdapter(List<String> images) {
        ImagesAdapter mImagesAdapter = new ImagesAdapter();
        WrappableGridLayoutManager mWrapHeightGridLayoutManager = new WrappableGridLayoutManager(mContext, 3);
        rv_img.setLayoutManager(mWrapHeightGridLayoutManager);
        mImagesAdapter.setNewData(ImageItem.replaceImage(images));
        rv_img.setAdapter(mImagesAdapter);
        mImagesAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (null != mClickImageListener)
                    mClickImageListener.onImageClick(adapter, view, position);
            }
        });
    }

    //图片适配
    private class ImagesAdapter extends BaseQuickAdapter<ImageItem, BaseViewHolder> {
        public ImagesAdapter() {
            super(R.layout.item_image_circle, null);
        }

        @Override
        protected void convert(BaseViewHolder baseViewHolder, ImageItem image) {
            Glide.with(mContext).load(image.path).into((ImageView) baseViewHolder.getView(R.id.iv_item));
            baseViewHolder.addOnClickListener(R.id.iv_item);
        }
    }

}
